<template>
  <div class="loginContainer">
    <van-icon name="arrow-left" size="40" class="van" color="#ccc" @click="back" />
    <div class="loginInner">
      <div class="login_header">
        <img src="./images/1.png" />
        <h2 class="login_logo">饭团外卖</h2>
        <div class="wrapper">
          <van-form class="formList">
            <van-field
              class="form"
              v-show="isphone"
              v-model="phone"
              name="手机号码"
              placeholder="请输入手机号码"
              maxlength="11"
              :rules="[{ required: true, message: '请输入正确手机号码' }]"
            />
            <van-field
              v-show="isphone"
              class="form"
              v-model="code"
              type="code"
              name="验证码"
              placeholder="请输入短信验证码"
              :rules="[{ required: true, message: '请填写验证码' }]"
            />
            <van-field
              v-show="islogin"
              class="form"
              v-model="name"
              name="用户名"
              placeholder="请输入用户名"
              :rules="[{ required: true, message: '请输入用户名' }]"
            />
            <van-field
              v-show="islogin"
              class="form"
              v-model="password"
              type="password"
              name="密码"
              placeholder="请输入密码"
              :rules="[{ required: true, message: '请填写密码' }]"
            />
            <div class="login_hint">
              温馨提示：未注册饭团外卖帐号的手机号，登录时将自动注册，且代表已同意
              <a href="javascript:;">《用户服务协议》</a>
            </div>
            <div class="formbox">
              <div class="password" @click="pwd">密码登录</div>
              <div class="reg" @click="reg">立即注册</div>
            </div>
            <div style="margin: 16px;">
              <button class="loginBtn" @click="login" v-show="isphone">登录</button>
            </div>
            <div style="margin: 16px;" v-show="islogin">
              <button class="loginBtn" @click="adminLogin">登录</button>
            </div>
            <a href="//rules-center.meituan.com/m?commonType=7" class="message">查看饭团协议与说明</a>
            <div
                class="sendCodeBtnText"
                v-show="isphone"
                :disabled="!rightPhone"
                @click="getCode"> 
                {{computeTime>0 ? `已发送(${computeTime}s)` : '发送验证码'}}
              </div>
          </van-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Vue from "vue";
import { Form, Field, Button, Toast ,Icon} from "vant";
Vue.use(Form);
Vue.use(Field);
Vue.use(Button);
Vue.use(Toast);
Vue.use(Icon);
export default {
  data() {
    return {
      phone: "", // 手机号
      code: "", // 短信验证码
      name: "", //用户名
      password: "", // 密码
      computeTime: 0, // 计时的时间
      isphone: true,
      islogin: false,
    };
  },
  created() {
    //底部栏消失
    this.$store.dispatch("footer/footernav", false);
  },
  beforeDestroy() {
    //销毁
    this.$store.dispatch("footer/footernav", true);
  },
  computed: {
    rightPhone() {
      //验证手机号码
      return  /^1\d{10}$/.test(this.phone);
      
    },
  },

  methods: {
    // 异步获取短信验证码
    async getCode(){
      console.log('123')
      if (!this.computeTime) {
         // 启动倒计时
        this.computeTime = 60;
        this.intervalId = setInterval(() => {
          this.computeTime--;
          if (this.computeTime <= 0) {
            // 停止计时
            clearInterval(this.intervalId);
          }
        }, 1000);
        //接口[http://localhost:4000/sendcode?phone=13716962779]
              // http://123.57.224.39:4000/
        const url = "http://123.57.224.39:4000/sendcode";
        const result = await this.$axios.get(
          url + "?" + "phone=" + `${this.phone}`
        );
        console.log(result.data.code);
        //获取验证码请求
        if (result.data.code === 1) {
          //手机号码错误时
          // 显示提示
          Toast.fail(result.data.msg);
          // 登录失败停止计时
          if (this.computeTime) {
            this.computeTime = 0;
            clearInterval(this.intervalId);
            this.intervalId = undefined;
          }
        }
      }
    },
    //异步登录处理
    async login() {
      let result;
      // 前台表单验证
      // 短信登陆
      const { phone, code } = this;
      if (!this.rightPhone) {
        // 手机号不正确
        Toast.fail("手机号不正确");
        return;
      } else if (!/^\d{6}$/.test(code)) {
        // 验证必须是6位数字
        Toast.fail("验证必须是6位数字");
        return;
      }
      // 发送ajax请求短信登陆 post请求
      const url = "http://123.57.224.39:4000/login_sms";
      result = await this.$axios.post(url, { phone, code });
      // Toast.fail(result.data.msg)
      console.log(result);
      // 停止计时
      if (this.computeTime) {
        this.computeTime = 0;
        clearInterval(this.intervalId);
        this.intervalId = undefined;
      }
      // 根据结果数据处理
      if (result.data.code === 0) {
        const userInfo = result.data.data;
        console.log("登录");
        // 将user保存到vuex的state
        this.$store.dispatch("user/recordUser", userInfo);
        // sessionStorage.setItem('recordUser',userInfo) //数据保存在vuex中
        // 去个人中心界面
        this.$router.push("/center");
      } else {
        Toast.fail(result.data.msg);
      }
      
    },
    async adminLogin() {
      //密码登录
      const { name, password } = this;
      if (!this.name) {
        // 用户名必须指定
        Toast.fail("用户名必须指定");
        return;
      } else if (!this.password) {
        // 密码必须指定
        Toast.fail("密码必须指定");
      }
      // 发送ajax请求密码登陆 http://localhost:3000/login_pwd
      let url = " http://123.57.224.39:4000/login_pwd";
      let res = await this.$axios.post(url, { name, password });
      console.log(res);
      
      // 根据结果数据处理
      if (res.data.code === 0) {
        const userInfo = res.data.data;
        // 将user保存到vuex的state
        // console.log('密码登录')
        // console.log(userInfo)
        this.$store.dispatch("user/recordUser", userInfo);
        // sessionStorage.setItem('recordUser',userInfo) //数据保存在vuex中
        // 去个人中心界面
        this.$router.push({ path: "/center", query: { a: 1 } });
        Toast.success("登录成功");
      } else {
        Toast.fail(res.data.msg);
      }
      
    },
    pwd() {
      //切换登录方式
      this.isphone = !this.isphone;
      this.islogin = !this.islogin;
    },
    //用户注册
    reg(){
      this.$router.push('./reg')
    },
    back(){
      this.$router.go(-1)
    }
  },
  
};
</script>

<style  lang="scss"   scoped>
.loginContainer {
  .van{
    margin-left: 10px;
    margin-top: 20px;
  }
  width: 100%;
  height: 100%;
  .loginInner {
    .login_header {
      padding-top: 60px;
      margin: 0 auto;
      img {
        width: 66px;
        height: 66px;
        margin-left: 152px;
      }
      .login_logo {
        text-align: center;
        font-size: 18.5px;
        font-weight: normal;
      }
      .wrapper {
        padding-top: 20px;
        .formList {
          position: relative;
          .form {
            font-size: 16px;
          }
          .login_hint {
            padding: 0 15px;
            margin-top: 12px;
            color: #999;
            font-size: 14px;
            line-height: 20px;
            a {
              color: rgb(223, 163, 24);
            }
          }
          .formbox {
            display: flex;
            .password {
              font-size: 14px;
              margin-left: 15px;
              margin-top: 15px;
            }
            .reg{
              font-size: 14px;
              margin-top: 15px;
              margin-left: 230px;
              color: rgb(223, 163, 24) ;
            }
          }
          .loginBtn {
            text-align: center;
            color: #999;
            background: #e5e5e5;
            height: 41px;
            line-height: 41px;
            border-radius: 5px;
            width: 100%;
            display: block;
            border: none;
            cursor: pointer;
            font-size: 16px;
          }
          .loginBtn1 {
            text-align: center;
            color: #999;
            background: #ffc135;
            height: 41px;
            line-height: 41px;
            border-radius: 5px;
            width: 100%;
            display: block;
            border: none;
            cursor: pointer;
            font-size: 16px;
          }
          .message {
            display: block;
            margin-top: 150px;
            text-decoration: underline;
            color: #999;
            font-size: 13.5px;
            text-align: center;
          }
          .sendCodeBtnText {
            display: block;
            text-align: center;
            color: #999;
            background: #e5e5e5;
            height: 24px;
            width: 78px;
            line-height: 24px;
            font-size: 12.8px;
            border-radius: 3px;
            position: absolute;
            right: 15px;
            top: 10px;
            border: none;
          }
        }
      }
    }
  }
}
</style>
